package com.ruoyi.demo.mapper;

/**
 * @author asus
 */
import com.ruoyi.demo.domain.MyMeet;
import com.ruoyi.demo.domain.vo.UserMeetInfoVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;

@Mapper
public interface MyMeetMapper {

    @Select("SELECT DISTINCT " +
            "d.device_num, " +
            "d.device_name, " +
            "d.status, " +
            "d.temp, " +
            "d.size, " +
            "d.is_active, " +
            "r.room_address " +
            "FROM device d " +
            "LEFT JOIN room r ON d.room_id = r.room_id " +
            "WHERE d.room_id = #{roomID}")
    List<MyMeet> findDevicesByRoomId(@Param("roomID") Long roomID);

    @Select("SELECT m.meet_id, m.theme, m.meet_content, m.start_time, m.end_time, m.`status`, " +
            "m.user_id, " +
            "mu.F_time, mu.L_time, mu.is_attented " +
            "FROM meet m " +
            "INNER JOIN meet_user mu ON m.meet_id = mu.meet_id " +
            "WHERE mu.user_id = #{userId} " +
            "ORDER BY m.meet_id")
    List<UserMeetInfoVO> selectUserMeetings(Long userId);

    @Select("SELECT d.device_num, d.device_name, d.status, d.temp, d.size, d.is_active, " +
           "r.room_address " +
           "FROM device d " +
           "INNER JOIN room r ON d.room_id = r.room_id " +
           "WHERE d.room_id = #{roomId}")
    List<MyMeet> findDevicesByMeetAndCreator(@Param("roomId") Long roomId);
    
    @Select("SELECT COUNT(1) " +
           "FROM meet " +
           "WHERE meet_id = #{meetId} " +
           "AND user_id = #{userId} " +
           "AND status IN (0, 1)")
    int checkMeetingCreator(@Param("meetId") Long meetId, @Param("userId") Long userId);

    @Update("UPDATE device " +
            "SET status = #{status}, " +
            "temp = #{temp}, " +
            "size = #{size}, " +
            "is_active = #{isActive} " +
            "WHERE device_num = #{deviceNum} " +
            "AND device_name = #{deviceName}")
    void updateDeviceStatus(MyMeet device);

    @Select("<script>" +
            "SELECT m.meet_id, m.theme, m.meet_content, m.start_time, m.end_time, m.num_respondents, m.`status`, " +
            "m.user_id, m.rejection, r.room_address, r.room_id, " +
            "mu.F_time, mu.L_time, mu.is_attented, su.user_name, " +
            "m.status as request_status " +
            "FROM meet m " +
            "INNER JOIN meet_user mu ON m.meet_id = mu.meet_id " +
            "LEFT JOIN room r ON m.room_id = r.room_id " +
            "LEFT JOIN sys_user su ON m.user_id = su.user_id " +
            "WHERE mu.user_id = #{userId} " +
            "<if test='theme != null and theme != \"\"'>" +
            "  AND m.theme LIKE concat('%', #{theme}, '%') " +
            "</if>" +
            "<if test='roomAddress != null and roomAddress != \"\"'>" +
            "  AND r.room_address LIKE concat('%', #{roomAddress}, '%') " +
            "</if>" +
            "<if test='status != null and status != \"\"'>" +
            "  AND m.status = #{status} " +
            "</if>" +
            "<if test='requestStatus != null and requestStatus != \"\"'>" +
            "  AND (SELECT mr2.status FROM meet_requests mr2 WHERE mr2.meet_id = m.meet_id ORDER BY mr2.created_at DESC LIMIT 1) = #{requestStatus} " +
            "</if>" +
            "ORDER BY m.start_time DESC" +
            "</script>")
    List<UserMeetInfoVO> selectMeetingList(@Param("theme") String theme,
                                           @Param("roomAddress") String roomAddress,
                                           @Param("status") String status,
                                           @Param("requestStatus") String requestStatus,
                                           @Param("userId") Long userId);

    @Select("<script>" +
            "SELECT m.meet_id, m.theme, m.meet_content, m.start_time, m.end_time, m.num_respondents, m.`status`, " +
            "m.user_id, m.rejection, r.room_address, r.room_id, su.user_name, " +
            "m.status as request_status " +
            "FROM meet m " +
            "LEFT JOIN room r ON m.room_id = r.room_id " +
            "LEFT JOIN sys_user su ON m.user_id = su.user_id " +
            "WHERE m.user_id = #{userId} " +
            "<if test='theme != null and theme != \"\"'>" +
            "  AND m.theme LIKE concat('%', #{theme}, '%') " +
            "</if>" +
            "<if test='roomAddress != null and roomAddress != \"\"'>" +
            "  AND r.room_address LIKE concat('%', #{roomAddress}, '%') " +
            "</if>" +
            "<if test='status != null and status != \"\"'>" +
            "  AND m.status = #{status} " +
            "</if>" +
            "<if test='requestStatus != null and requestStatus != \"\"'>" +
            "  AND (SELECT mr2.status FROM meet_requests mr2 WHERE mr2.meet_id = m.meet_id ORDER BY mr2.created_at DESC LIMIT 1) = #{requestStatus} " +
            "</if>" +
            "ORDER BY m.start_time DESC" +
            "</script>")
    List<UserMeetInfoVO> selectMyMeetList(@Param("theme") String theme,
                                           @Param("roomAddress") String roomAddress,
                                           @Param("status") String status,
                                           @Param("requestStatus") String requestStatus,
                                           @Param("userId") Long userId);


    @Update("DELETE FROM meet WHERE meet_id = #{meetId}")
    void deleteMeeting(@Param("meetId") Long meetId);
}
